﻿using BySoft.Server.Business;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class ProcessEdit : AuthBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
            Master.ValidationGroup = "gSubmit";
        }

        private void BindData()
        {
            string strCode = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id").Trim());
            if (!string.IsNullOrEmpty(strCode))
            {
                MdProcessEntity en = MdProcess.GetMdProcessByID(strCode);
                if (en != null)
                {
                    this.lblProcessID.Text = en.ProcessID;
                    this.txtProcessName.Text = en.ProcessName;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(CurrentUser.UserID))
                {
                    UIHelper.AlertMessage(this.Page, "等待时间太久，请重新填写工位站点资料！");
                    return;
                }
                if (!string.IsNullOrEmpty(this.lblProcessID.Text.Trim()))
                {
                    if (MdProcess.IsProcessExists(this.txtProcessName.Text.Trim(), this.lblProcessID.Text.Trim()))
                    {
                        UIHelper.AlertMessage(this.Page, "工位名称已存在，请重新填写！");
                        return;
                    }
                    MdProcessEntity product = new MdProcessEntity();
                    product.ProcessID = this.lblProcessID.Text.Trim();
                    product.ProcessName = this.txtProcessName.Text.Trim();
                    product.Status = "1";
                    product.ModifyDate = DateTime.Now;
                    product.ModifyUserID = CurrentUser.UserID;
                    product.ModifyUserName = CurrentUser.UserName;

                    if (MdProcess.Update(product))
                    {
                        base.LogAction(BaseEnum.ActionType.Modify, product.ProcessID, true, null, new EntityBase[] { product });
                        UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "../MasteData/ProcessList.aspx");
                    }
                    else
                    {
                        UIHelper.AlertMessage(this.Page, Message.SaveFail);
                    }
                }

            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, FomatString(ex.Message.ToString()));
            }
        }
    }
}